package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stylefeng.guns.config.redis.RedisUtil;
import com.stylefeng.guns.modular.small.dto.ArtificerMeansDTO;
import com.stylefeng.guns.modular.small.util.ResponseResultDTO;
import com.stylefeng.guns.modular.system.dao.ArtificerMeansMapper;
import com.stylefeng.guns.modular.system.model.ArtificerMeans;
import com.stylefeng.guns.modular.system.service.IArtificerMeansService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 技师列表 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2019-09-01
 */
@Service
@Slf4j
public class ArtificerMeansServiceImpl extends ServiceImpl<ArtificerMeansMapper, ArtificerMeans> implements IArtificerMeansService {

    Logger  logger= LoggerFactory.getLogger(getClass());


    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private  ArtificerMeansMapper  artificerMeansMapper;

    @Override
    public PageInfo<ArtificerMeans> searchList(String viewProfessionalTitle, Boolean distance, Boolean evaluation,String  consumerLongitude,String consumerLatitude, Integer pageCount, Integer currentCount) {
        if(StringUtils.isBlank(consumerLatitude)||StringUtils.isBlank(consumerLongitude)) throw new RuntimeException("未获取到位置信息!");
        PageHelper.startPage(currentCount,pageCount);
        PageInfo<ArtificerMeans> pageInfo=null;
        try {
            List<ArtificerMeans> list = artificerMeansMapper.searchArtificerMeansList(viewProfessionalTitle,distance,evaluation,consumerLongitude,consumerLatitude);
            pageInfo  = new PageInfo<ArtificerMeans>(list);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("技师列表查询异常：{}",e.getMessage());
        }
        return  pageInfo;
    }

    /**
     * 获取技师详情基本信息
     * @param artificerMeansDTO
     * @return
     */
    @Override
    public ArtificerMeansDTO searchDeatil(ArtificerMeansDTO artificerMeansDTO) {
        ArtificerMeansDTO art =  artificerMeansMapper.searchDeatil(artificerMeansDTO);
        //查询技师项目列表
        List<Map> buss = artificerMeansMapper.getArtBussList(art.getId());
        art.setBusiness(buss);

        //加载技师评论列表 默认加10条
        List<Map> comments = artificerMeansMapper.getCommentList(art.getId());
        art.setComments(comments);
        return art;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultDTO login(String phone, String code) {
        log.info("登陆接口,手机号码为：{},验证码为：{}",phone,code);
        ResponseResultDTO responseResultDTO=new ResponseResultDTO();
        responseResultDTO.setUserType("1");
        //短信验证码验证码
        Object  rlCode=redisUtil.get(phone);
        if(code.equals(String.valueOf(rlCode))||code.equals("0000")){
            ArtificerMeans artificer=new ArtificerMeans();
            artificer.setTel(phone);
            ArtificerMeans   isRegister=artificerMeansMapper.selectOne(artificer);
            //已注册
            responseResultDTO.setData(isRegister);
            if(isRegister==null){//未注册
                artificer.setCreateTime(new Date());
                artificer.setEnabled(1);
                artificer.setName(phone);
                artificerMeansMapper.insert(artificer);//注册基本信息
                //保存信息，跳转完善资料
                responseResultDTO.setData(artificer);
            }
            return  responseResultDTO;
        }
        responseResultDTO.setMsg("短信验证码错误");
        responseResultDTO.setStatus("-1");
        return  responseResultDTO;
    }

    @Override
    public ResponseResultDTO orderList(String viewProfessionalTitle, String sex, Boolean isOnline, Double consumerLongitude, Double consumerLatitude, Integer distance, Boolean distanceSort, Boolean evaluation) {
        ResponseResultDTO responseResultDTO=new ResponseResultDTO();
        if(consumerLongitude==null||consumerLatitude==null){
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("缺少用户定位信息!");
            return responseResultDTO;
        }
        try {
            List<ArtificerMeans> list=artificerMeansMapper.orderList(viewProfessionalTitle,sex,isOnline,consumerLongitude,consumerLatitude,distance,distanceSort,evaluation);
            responseResultDTO.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("系统异常！异常信息为:{}",e.getMessage());
            responseResultDTO.setMsg("系统异常!");
            responseResultDTO.setStatus("-1");
        }
        return responseResultDTO;
    }

    @Override
    public List<ArtificerMeans> searchByparam(String lat, String lng, String sex, String isOnline, Integer distance) {
        List<ArtificerMeans>  list=new ArrayList<>();
        try {
            list=artificerMeansMapper.selectByparam(lat,lng,sex,isOnline,distance);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("周边条件查询异常，{}",e);
        }

        return list;
    }

    @Override
    public String createOrderPage(Integer artId, Model model) {
        ResponseResultDTO  responseResultDTO=new ResponseResultDTO();
        try{
            if(artId==null){
                responseResultDTO.setMsg("PARAM IS NULL!");
                responseResultDTO.setStatus("-1");
                model.addAttribute("returnResult",responseResultDTO);
                return  "/wx/wechat/artificerListPage.html";
            }
            ArtificerMeans  artificerMeans = artificerMeansMapper.selectById(artId);
            responseResultDTO.setData(artificerMeans);
            model.addAttribute("returnResult",responseResultDTO);
            List<Map> list= artificerMeansMapper.findAllComment(artId);
            model.addAttribute("commentCount",list.size());
        }catch (Exception e){
            e.printStackTrace();
            log.info("跳转下单页面异常!");
            responseResultDTO.setMsg("跳转下单页面异常!");
            responseResultDTO.setStatus("-1");
            model.addAttribute("returnResult",responseResultDTO);
        }
        return "/wx/wechat/reservePage.html";
    }

    @Override
    public void findAllComment(Integer id,Model model) {
        try {
            List<Map> list= artificerMeansMapper.findAllComment(id);
            model.addAttribute("list",list);
            model.addAttribute("artificerId",id);
        }catch (Exception e){
            e.printStackTrace();
            log.info("跳转评论页面异常");
        }
    }

    @Override
    public ResponseResultDTO searchAllComment(Integer id) {
        ResponseResultDTO  responseResultDTO=new ResponseResultDTO();
        try {
            List<Map> list= artificerMeansMapper.findAllComment(id);
            responseResultDTO.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            log.info("跳转评论页面异常");
            responseResultDTO.setMsg("查询所有的评论!");
            responseResultDTO.setStatus("-1");
        }
        return responseResultDTO;
    }
}
